package com.huaxin.common.Deprecated;

import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huaxin.common.BaseActivity;
import com.huaxin.common.BaseFragment;
import com.huaxin.common.Deprecated.BaseImp;
import com.huaxin.common.Deprecated.MapCenter;
import com.huaxin.common.tools.Log;

/**
 * Created by hebing on 2016/4/7.
 */
public class BaseHelper {
    protected final static String TAG = "BaseHelper";
    /**
     * 用于保存界面所有view元素，如果可能
     */
    ArrayMap<Integer, View> mViews;

    MapCenter.ViewBuilder builder;

    boolean fromApp;

    BaseActivity activity;

    BaseFragment fragment;

    BaseHelper(BaseActivity activity) {
        fromApp = true;
        this.activity = activity;
    }

    BaseHelper(BaseFragment fragment) {
        fromApp = false;
        this.fragment = fragment;
    }

    void recycle() {
        if (builder != null) builder.unBind();
        if (mViews != null) mViews.clear();
        activity = null;
        fragment = null;
        builder = null;
        mViews = null;
    }

    /**
     * 一般界面被拦截，则会影响部分流程
     *
     * @param clazz
     * @param baseImp
     */
    void bind(Class clazz, BaseImp baseImp) {
        builder = MapCenter.getBuild(clazz);
        if (builder != null) builder.onBind(baseImp);
    }

    /**
     * 可拦截activity或者fragment的界面布局ID
     *
     * @return
     */
    int getContentViewId() {
        if (builder != null) return builder.generatorViewId();
        return 0;
    }

    void build() {
        if (builder != null) builder.onBuild();
    }

    void onSaveInstanceState(Bundle outState) {
        if (builder != null) builder.onSaveInstanceState(outState);
    }

    void onRestoreInstanceState(Bundle savedInstanceState) {
        if (builder != null) builder.onRestoreInstanceState(savedInstanceState);
    }

    void onResume() {
        if (builder != null) builder.onResume();
    }

    void onPause() {
        if (builder != null) builder.onPause();
    }

    View findViewById(int id) {
        if (fromApp) {
            return activity.findViewById(id);
        } else {
            return fragment.mContentView.findViewById(id);
        }
    }

    void setView(int id, View view) {
        if (mViews == null) mViews = new ArrayMap<>();
        if (mViews.containsKey(id)) Log.i(TAG, "replace :" + id);
        mViews.put(id, view);
    }

    /******************************
     * ActivityImp 定义方法
     ******************************/
    void initView() {
        //do nothing
    }

    void initData() {
        //do nothing
    }

    public View getView(int id) {
        return getView(null, id);
    }

    public View getView(View parent, int id) {
        //if find it in map cache
        if (mViews != null && mViews.containsKey(id)) {
            return mViews.get(id);
        }

        final View view;
        if (parent != null) {
            view = parent.findViewById(id);
        } else {
            view = findViewById(id);
        }
        if (view == null) {
            throw new RuntimeException("Can't find view by id : " + id + ",have you had defined it?");
        } else {
            setView(id, view);
        }
        return view;
    }

    //extra
    public TextView getTextView(int id) {
        final View get = getView(id);
        if (get instanceof TextView) {
            return (TextView) get;
        } else {
            throw new RuntimeException("can't cast " + get + " to TextView!");
        }
    }

    public EditText getEditText(int id) {
        final View get = getView(id);
        if (get instanceof EditText) {
            return (EditText) get;
        } else {
            throw new RuntimeException("can't cast " + get + " to EditText!");
        }
    }

    public LinearLayout getLinearLayout(int id) {
        final View get = getView(id);
        if (get instanceof LinearLayout) {
            return (LinearLayout) get;
        } else {
            throw new RuntimeException("can't cast " + get + " to LinearLayout!");
        }
    }

    public RelativeLayout getRelativeLayout(int id) {
        final View get = getView(id);
        if (get instanceof RelativeLayout) {
            return (RelativeLayout) get;
        } else {
            throw new RuntimeException("can't cast " + get + " to RelativeLayout!");
        }
    }

    public FrameLayout getFrameLayout(int id) {
        final View get = getView(id);
        if (get instanceof FrameLayout) {
            return (FrameLayout) get;
        } else {
            throw new RuntimeException("can't cast " + get + " to FrameLayout!");
        }
    }

    public ListView getListView(int id) {
        final View get = getView(id);
        if (get instanceof ListView) {
            return (ListView) get;
        } else {
            throw new RuntimeException("can't cast " + get + " to ListView!");
        }
    }
}
